﻿Public Class frmJournalVentes
    Private szSQL As String
    Private rs As ADODB.Recordset = Nothing
    Private m_SortingColumn As ColumnHeader

    Private Sub frmJournalVentes_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        TE_DATE.Value = Now
        BO_VENTES.Text = "Agence " & gUserAgence
        chkDateValeur.Checked = True
        InitListeVentes()
    End Sub

    Private Sub TE_DATE_ValueChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TE_DATE.ValueChanged
        InitListeVentes()
    End Sub

    Private Sub InitListeVentes()

        Dim rs2 As ADODB.Recordset = Nothing
        Dim nb As Integer = 0
        Dim total As Decimal = 0
        Dim valeur As Decimal = 0
        Dim nom As String
        Dim permis As String

        ' effacer la listview
        LV_JOURNAL.Items.Clear()

        'RAZ total
        total = 0

        ' par date de saisie ?
        If chkDateValeur.Checked = False Then
            szSQL = "CALL spGet_ELEVES_CPT_byDATE(" & gUserAgence & ",'" & Format(TE_DATE.Value, "yyyy/MM/dd") & "','DEB')"
            BO_VENTES.Enabled = False
            BO_VENTES_ALL.Enabled = False
        Else
            szSQL = "CALL spGet_ELEVES_CPT_byVALEUR(" & gUserAgence & ",'" & Format(TE_DATE.Value, "yyyy/MM/dd") & "','DEB')"
            BO_VENTES.Enabled = True
            BO_VENTES_ALL.Enabled = True
        End If
        ' Lire les débits
        GetRecordset(ctDSN_AEM, szSQL, rs)

        ' Load the data.
        Do While Not rs.EOF
            ' Lire la fiche Eleves
            szSQL = "CALL spGet_ELEVES_byCD_CLI(" & nv(rs.Fields("cd_cli").Value) & ")"
            GetRecordset(ctDSN_AEM, szSQL, rs2)
            If rs2.RecordCount > 0 Then
                nom = rs2.Fields("nompren").Value
                permis = rs2.Fields("permis_demande").Value
            Else
                nom = "non trouvé"
                permis = " "
            End If
            LV_JOURNAL.Items.Add(rs.Fields("cd_cli").Value)
            LV_JOURNAL.Items(nb).SubItems.Add(nom)
            LV_JOURNAL.Items(nb).SubItems.Add(permis)
            LV_JOURNAL.Items(nb).SubItems.Add(rs.Fields("intitule").Value)
            LV_JOURNAL.Items(nb).SubItems.Add(rs.Fields("debit").Value)
            LV_JOURNAL.Items(nb).SubItems.Add(rs.Fields("mtval").Value)
            LV_JOURNAL.Items(nb).SubItems.Add(rs.Fields("cd_uti").Value)
            total = total + rs.Fields("debit").Value
            valeur = valeur + rs.Fields("mtval").Value
            rs.MoveNext()
            nb = nb + 1
        Loop
        ' Close the recordset AND connection.
        rs.Close()

        TE_TOTAL.Text = total & " €"
        TE_VALEUR.Text = valeur & " €"
        If nb > 0 Then
            LB_NbLignes.Text = CStr(nb) & " lignes"
            BO_VENTES.Enabled = True
            BO_VENTES_ALL.Enabled = True
        Else
            LB_NbLignes.Text = "0 ligne"
            BO_VENTES.Enabled = False
            BO_VENTES_ALL.Enabled = False
        End If

    End Sub

    Private Sub chkDateValeur_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles chkDateValeur.CheckedChanged
        InitListeVentes()
    End Sub
   
    Private Sub BO_VENTES_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BO_VENTES.Click
        gDate_debut = TE_DATE.Value
        If chkDateValeur.Checked = False Then
            ' édition journal des ventes par date d'operation par Agence
            FrmViewReport.pCodeEtat = 16
        Else
            ' édition journal des ventes par date de valeur par Agence
            FrmViewReport.pCodeEtat = 46
        End If
        FrmViewReport.pParamRPT = Format(TE_DATE.Value, "dd/MM/yyyy")
        FrmViewReport.ShowDialog()

    End Sub

    Private Sub BO_VENTES_ALL_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BO_VENTES_ALL.Click
        gDate_debut = TE_DATE.Value
        If chkDateValeur.Checked = False Then
            ' édition journal des ventes (date operation) de toutes les agences
            FrmViewReport.pCodeEtat = 17
        Else
            ' édition journal des ventes (date valeur) de toutes les agences
            FrmViewReport.pCodeEtat = 47
        End If
        FrmViewReport.pParamRPT = Format(TE_DATE.Value, "dd/MM/yyyy")
        FrmViewReport.ShowDialog()

    End Sub

    Private Sub LV_JOURNAL_ColumnClick(ByVal sender As Object, ByVal e As System.Windows.Forms.ColumnClickEventArgs) Handles LV_JOURNAL.ColumnClick
        SortListView(LV_JOURNAL, m_SortingColumn, e.Column)
    End Sub
End Class